// 本文件用于：
import { _post } from '@/lib/request';
import store from '@/store';
import { setDisplayArea, setGlobalMask } from '@/store/slice/global';
import { App, Button, Card, Space, message as nrMessage } from 'antd';
import { memo, useState } from 'react';
import { v4 } from 'uuid';
import AAA from './aaa';
import { setNumA } from './testImport';

export default memo(() => {
  const { message } = App.useApp();

  const [sessionStorageInfo, setSessionStorageInfo] = useState('');

  /** 受全局App组件控制的message */
  const appMsg = () => {
    message.success('111');
  };

  /** 无限制的message */
  const normalMessag = () => {
    nrMessage.success('111');
  };

  const testMask = (type) => {
    store.dispatch(
      setGlobalMask({ show: true, type, text: type === 'fullscreen' ? '两秒后自动关闭' : '' }),
    );

    setTimeout(() => {
      store.dispatch(setGlobalMask({ show: false }));
    }, 2000);
  };

  const testXH = () => {
    const a = {
      x: 1,
    };

    const b = {
      y: 1,
    };

    Object.setPrototypeOf(b, a);

    console.log('a', a);
    console.log('b', b);

    console.log('使用 for in');
    for (let key in b) {
      console.log('key', key);
    }

    console.log('使用 Object.keys');
    Object.keys(b).forEach((key) => {
      console.log('key', key);
    });

    console.log('使用 Object.values');
    Object.values(a).forEach((key) => {
      console.log('val', key);
    });
  };

  const testXHArray = () => {
    const a = ['x', 'y', 'z'];
    a['xss'] = 1;

    console.log('a', a);

    console.log('使用 for in');
    for (let key in a) {
      console.log('key', key);
    }

    console.log('使用 Object.keys');
    Object.keys(a).forEach((key) => {
      console.log('key', key);
    });

    console.log('使用 Object.values');
    Object.values(a).forEach((key) => {
      console.log('val', key);
    });

    console.log('使用 for of');
    for (let key of a) {
      console.log('key', key);
    }
  };

  const cgVisiable = (key: string) => {
    store.dispatch(setDisplayArea(key));
  };

  const aaa = () => {
    var data = JSON.stringify({
      id: '46',
      varData:
        '{"city_name":"宿迁市,西南大区,临沂市,上海,衢州市,鲁豫皖,中山市,上海特区,潍坊市,苏皖鄂大区,沈阳市,浙赣湘大区,乌鲁木齐市,华北大区,乐山市,华南大区,济宁市,全国,上海市,昆山市,郑州市,无锡市,苏州市,武汉市,西安市,杭州市,南昌市,镇江市,周口市,商丘市,成都市,开封市,广州市,北京市,温州市,湖州市,合肥市,南京市,台州市,金华市,长春市,深圳市,天津市,济南市,常州市,绍兴市,华中大区,华东大区,宁波市,昆明市,扬州市,青岛市,长沙市,嘉兴市,南通市,遵义市,贵阳市,廊坊市,徐州市,泰州市,邯郸市,淮南市,佛山市,东莞市,惠州市,海口市,三亚市,南宁市,柳州市,重庆市,太原市,石家庄市,厦门市,福州市,舟山市,珠海市,大连市,烟台市,蚌埠市,滁州市,芜湖市,阜阳市,泰安市,poc,soc-pn,soc-ph,soc-ps,soc-c,soc-pj,soc-pz,其他,浙江,江苏,安徽,沪豫,poc大都会,盐城市","driver_type_desc":"汇总","third_platform":"享道,高德,携程商旅,滴滴,平安,阿里欢行,翼支付,游上海,巴士管家,关爱通,OK支付,绿城云助,大汉三通,heycars,国铁出行,微信小程序,金保证（光大、招行等银行客户端）,易快报,易快报（预发布环境）,支付宝小程序,问号用车,差旅壹号,伙力专车,车马出行,新企业级,航班管家,12306,腾讯地图,百度地图,途径用车,云闪付出行服务,百福得,分贝通,同程旅行,招商银行用车服务,公务用车易-个人出行,哈啰出行,安徽政务保障用车,德邦欢行,喵街打车服务,阿里商旅,每刻报销,飞常准,航旅纵横,胜意科技,飞扬商务管理,携程专车接送机,花小猪,招银网络用车平台,飞泊通,高德特惠一口价,吉利商旅,汇联易,领端科技,携程商旅（新）,够谱出行,中远海运,一喂顺风车,凯航商旅,民生银行,文华商旅,东航电商用车,携程打车（新）,随申行网约车,招商银行股份有限公司Z,梓如商旅,差旅管家,虹桥机场,同港出行,唯品会—加班打车,浦东国际人才驿站,中旅商旅,腾讯打车","car_level":"汇总","date":"2023-11-08 ~ 2023-11-09"}',
      varDataText:
        '{"城市":"宿迁市,西南大区,临沂市,上海,衢州市,鲁豫皖,中山市,上海特区,潍坊市,苏皖鄂大区,沈阳市,浙赣湘大区,乌鲁木齐市,华北大区,乐山市,华南大区,济宁市,全国,上海市,昆山市,郑州市,无锡市,苏州市,武汉市,西安市,杭州市,南昌市,镇江市,周口市,商丘市,成都市,开封市,广州市,北京市,温州市,湖州市,合肥市,南京市,台州市,金华市,长春市,深圳市,天津市,济南市,常州市,绍兴市,华中大区,华东大区,宁波市,昆明市,扬州市,青岛市,长沙市,嘉兴市,南通市,遵义市,贵阳市,廊坊市,徐州市,泰州市,邯郸市,淮南市,佛山市,东莞市,惠州市,海口市,三亚市,南宁市,柳州市,重庆市,太原市,石家庄市,厦门市,福州市,舟山市,珠海市,大连市,烟台市,蚌埠市,滁州市,芜湖市,阜阳市,泰安市,poc,soc-pn,soc-ph,soc-ps,soc-c,soc-pj,soc-pz,其他,浙江,江苏,安徽,沪豫,poc大都会,盐城市","运力类型":"汇总","平台":"享道,高德,携程商旅,滴滴,平安,阿里欢行,翼支付,游上海,巴士管家,关爱通,OK支付,绿城云助,大汉三通,heycars,国铁出行,微信小程序,金保证（光大、招行等银行客户端）,易快报,易快报（预发布环境）,支付宝小程序,问号用车,差旅壹号,伙力专车,车马出行,新企业级,航班管家,12306,腾讯地图,百度地图,途径用车,云闪付出行服务,百福得,分贝通,同程旅行,招商银行用车服务,公务用车易-个人出行,哈啰出行,安徽政务保障用车,德邦欢行,喵街打车服务,阿里商旅,每刻报销,飞常准,航旅纵横,胜意科技,飞扬商务管理,携程专车接送机,花小猪,招银网络用车平台,飞泊通,高德特惠一口价,吉利商旅,汇联易,领端科技,携程商旅（新）,够谱出行,中远海运,一喂顺风车,凯航商旅,民生银行,文华商旅,东航电商用车,携程打车（新）,随申行网约车,招商银行股份有限公司Z,梓如商旅,差旅管家,虹桥机场,同港出行,唯品会—加班打车,浦东国际人才驿站,中旅商旅,腾讯打车","车级":"汇总","日期":"2023-11-08 ~ 2023-11-09"}',
      varDate: '{}',
      userInfo:
        '{"sub":"ca11d312-c545-4c17-a4c6-10abf8bf4ecd","email_verified":false,"name":"卢强","id_token":"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIi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wD6nfIaPA2gpWfUbELlQRjeSWg7FBAUJ9BCK5Tk7074hqcDD6hZ2XG4SIndOHMVDRVeo-KXtr1eMrJCRbNLHvqCBg","loginRequestInfo":{"password":"a22a007a76b1889082fb78bf5a8b9220","loginTime":"2023-11-09 10:23:38","loginType":1,"phone":"18521503001","username":"luqiang"},"preferred_username":"LUQIANG","given_name":"强","family_name":"卢","email":"luqiang@saicmobility.com","um":"LUQIANG","path":""}',
      limit: 20,
      page: 144,
    });

    _post(
      'https://d.saicmobility.com/datacore-report/sql/data',
      {
        data: data,
      },
      {
        Cookie: 'SERVERID=fdb7de4dce696aef5bdffd1951682236|1699496836|1699496836',
      },
    );
  };

  const testVar = () => {
    const cloArr: any[] = [];
    const cloArr2: any[] = [];

    for (var i = 0; i < 5; i++) {
      cloArr.push(() => {
        console.log(i);
      });

      var objI = { i };
      cloArr2.push(() => {
        console.log(objI);
      });
    }

    cloArr[2]();
    cloArr2[2]();
  };

  const testDynamicFun = () => {
    function doTest(staticTpl: any, ...args: any[]) {
      console.log('staticTpl', staticTpl);
      console.log('args', args);
    }

    const val1 = 'B';
    const val2 = 'D';

    doTest`A${val1}C${val2}E`;
  };

  const doSetSessionStorage = () => {
    sessionStorage.setItem('test-key', v4());
  };

  const doGetSessionStorage = () => {
    setSessionStorageInfo(sessionStorage.getItem('test-key') || '');
  };

  const openNewThis = () => {
    window.open(window.location.href);
  };

  const openBaidu = () => {
    window.open('https://www.baidu.com/');
  };

  function createSuperLabel(url) {
    let a = document.createElement('a');
    a.setAttribute('href', url);
    a.setAttribute('target', '_blank');
    // 防止反复添加
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  }

  const getMax = (arr: number[], start: number, end: number): number => {
    if (start === end) return arr[start];

    const middle = start + ((end - start) >> 1);
    const left = getMax(arr, start, middle);
    const right = getMax(arr, middle + 1, end);
    return Math.max(left, right);
  };

  const doGetMax = () => {
    const tmpList: any = [];
    for (let i = 0; i < 10; i++) {
      tmpList.push(Math.floor(Math.random() * 100));
    }
    console.log('tmpList', tmpList);
    console.log('getMax(tmpList)', getMax(tmpList, 0, tmpList.length - 1));
  };

  return (
    <>
      <Card>
        <Space>
          <Button onClick={appMsg}>限制次数的message</Button>
          <Button onClick={normalMessag}>普通的message</Button>
        </Space>
      </Card>

      <Card>
        <Space>
          <Button onClick={() => testMask('fullscreen')}>测试全屏遮罩</Button>
          <Button onClick={() => testMask('app')}>测试主要区域遮罩</Button>
        </Space>
      </Card>

      <Card>
        <Space>
          <Button onClick={testXH}>测试读取key（对象）</Button>
          <Button onClick={testXHArray}>测试读取key（数组）</Button>
        </Space>
      </Card>

      <Card>
        <Space>
          <Button onClick={() => cgVisiable('header')}>隐藏/展示顶部</Button>
          <Button onClick={() => cgVisiable('menu')}>隐藏/展示菜单</Button>
        </Space>
      </Card>

      <Card>
        <Space>
          <Button onClick={aaa}>测试1111</Button>
          <Button onClick={testVar}>测试var</Button>
          <Button onClick={testDynamicFun}>测试动态字符串传参</Button>
        </Space>
      </Card>

      <Card>
        <Space>
          <Button onClick={doSetSessionStorage}>设置sessionStorage</Button>
          <Button onClick={doGetSessionStorage}>获取sessionStorage</Button>
          <Button onClick={openNewThis}>打开新界面(会带过去)</Button>
          <Button
            onClick={() => {
              createSuperLabel('/');
            }}
          >
            打开新界面（带不过去）
          </Button>
          <a href="/" title="打开新界面" target="_blank">
            打开新界面（带不过去）
          </a>
          <Button onClick={openBaidu}>打开百度（会带过去）</Button>
          <br />
          <div>当前sessionStorage信息：{sessionStorageInfo}</div>
        </Space>
      </Card>

      <Card>
        <Space>
          <Button
            onClick={() => {
              setNumA(12);
            }}
          >
            设置numA
          </Button>

          <AAA />
        </Space>
      </Card>

      <Card>
        <Space>
          <Button onClick={doGetMax}>获取最大值</Button>
        </Space>
      </Card>
    </>
  );
});
